import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.swing.*;
import java.awt.event.*;

public class WriteXMLFile extends JFrame implements ActionListener {
	JLabel label1, label2,	label3, label4;
	JTextField field1, field2, field3;
	JButton button1, button2, button3, button4, button5;
	
	public void search(){
		try{
			int sw=0;
		String nifBusco=field3.getText();
		String fichero="alumnos.xml";
		File fXmlFile=new File(fichero);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
		NodeList list=doc.getElementsByTagName("alumno");
		
		int numeronodos=list.getLength();
		for (int i=0;i<numeronodos;i++){
			Node nodo =list.item(i);
			
			if(nodo.getNodeType()==Node.ELEMENT_NODE){
				Element elemento=(Element) nodo;
				String nombreleo=elemento.getElementsByTagName("nombre").item(0).getTextContent();
				String apellidoleo=elemento.getElementsByTagName("apellido").item(0).getTextContent();
				String nifleo=elemento.getElementsByTagName("nif").item(0).getTextContent();
				if(nifleo.compareTo(nifBusco)==0)
				{
					field1.setText(nombreleo);
					field2.setText(apellidoleo);
					sw=1;
					break;
				}
			}
		}
		if(sw==0){
			field1.setText("");
			field2.setText("");
			JOptionPane.showMessageDialog (null,"no encontrado","Information",JOptionPane.INFORMATION_MESSAGE);
				}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null,e,"Informacion",JOptionPane.INFORMATION_MESSAGE);
			}
	}
	public void modify(){

		try{
			int sw=0;
		String nifBusco=field3.getText();
		String fichero="alumnos.xml";
		File fXmlFile=new File(fichero);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
		Element documentElement=doc.getDocumentElement();
		NodeList list=doc.getElementsByTagName("alumno");
		
		int numeronodos=list.getLength();
		for (int i=0;i<numeronodos;i++){
			Node nodo =list.item(i);
			
			if(nodo.getNodeType()==Node.ELEMENT_NODE){
				Element elemento=(Element) nodo;
				String nifleo=elemento.getElementsByTagName("nif").item(0).getTextContent();
				if(nifleo.compareTo(nifBusco)==0)
				{
					elemento.getElementsByTagName("nombre").item(0).setTextContent(field1.getText());
					elemento.getElementsByTagName("apellido").item(0).setTextContent(field2.getText());
					doc.replaceChild(documentElement,documentElement);
					Transformer tFormer=TransformerFactory.newInstance().newTransformer();
					tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
					Source source = new DOMSource(doc);
					Result result = new StreamResult(fichero);
					tFormer.transform(source, result);
					sw=1;
					break;
				}
			}
		}
		if(sw==0){
			field1.setText("");
			field2.setText("");
			JOptionPane.showMessageDialog (null,"no encontrado","Information",JOptionPane.INFORMATION_MESSAGE);
				}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null,e,"Informacion",JOptionPane.INFORMATION_MESSAGE);
			}
	}
	public void delete(){
		try{
			int sw=0;
		String nifBusco=field3.getText();
		String fichero="alumnos.xml";
		File fXmlFile=new File(fichero);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
		Element documentElement=doc.getDocumentElement();
		NodeList list=doc.getElementsByTagName("alumno");
		
		int numeronodos=list.getLength();
		for (int i=0;i<numeronodos;i++){
			Node nodo =list.item(i);
			
			if(nodo.getNodeType()==Node.ELEMENT_NODE){
				Element elemento=(Element) nodo;
				String nifleo=elemento.getElementsByTagName("nif").item(0).getTextContent();
				if(nifleo.compareTo(nifBusco)==0)
				{
					documentElement.removeChild(nodo);
					doc.replaceChild(documentElement,documentElement);
					Transformer tFormer=TransformerFactory.newInstance().newTransformer();
					tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
					Source source = new DOMSource(doc);
					Result result = new StreamResult(fichero);
					tFormer.transform(source, result);
					sw=1;
					break;
				}
			}
		}
		if(sw==0){
			field1.setText("");
			field2.setText("");
			JOptionPane.showMessageDialog (null,"no encontrado","Information",JOptionPane.INFORMATION_MESSAGE);
				}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null,e,"Informacion",JOptionPane.INFORMATION_MESSAGE);
			}
	}
	public void add(){
		
		try{
			int sw=0;
		String nifBusco=field3.getText();
		String fichero="alumnos.xml";
		File fXmlFile=new File(fichero);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
		Element documentElement=doc.getDocumentElement();
		
		Element nombre=doc.createElement("nombre");
		Element apellido=doc.createElement("apellido");
		Element nif=doc.createElement("nif");
		nombre.appendChild(doc.createTextNode(field1.getText()));
		apellido.appendChild(doc.createTextNode(field2.getText()));
		nif.appendChild(doc.createTextNode(field3.getText()));
		
		Element nuevoAlumno=doc.createElement("alumno");
		nuevoAlumno.appendChild(nombre);
		nuevoAlumno.appendChild(apellido);
		nuevoAlumno.appendChild(nif);
		
		documentElement.appendChild(nuevoAlumno);
		doc.replaceChild(documentElement, documentElement);
		
		Transformer tFormer=TransformerFactory.newInstance().newTransformer();
		tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
		Source source = new DOMSource(doc);
		Result result = new StreamResult(fichero);
		tFormer.transform(source, result);
		
		}
		catch(Exception e){

			}
	}
	public void GUI(){
		setLayout (null);
		label1=new JLabel("Nombre");
		label2=new JLabel("Apellido");
		label3=new JLabel("NIF");
		label4=new JLabel("By Igor Prieto");
		field1=new JTextField();
		field2=new JTextField();
		field3=new JTextField();
		button1=new JButton("Añadir");
		button2=new JButton("Buscar");
		button3=new JButton("Eliminar");
		button4=new JButton("Modificar");	
		button5=new JButton("Instrucciones");
	}
	public void position(){
		label1.setBounds(30, 20, 100, 20);
		label2.setBounds(30, 50, 100, 20);
		label3.setBounds(30, 80, 100, 20);
		label4.setBounds(107, 350, 120, 20);
		field1.setBounds(140, 20, 100, 20);
		field2.setBounds(140, 50, 100, 20);
		field3.setBounds(140, 80, 100, 20);
		button1.setBounds(100, 150, 100, 20);
		button2.setBounds(100, 190, 100, 20);
		button3.setBounds(100, 230, 100, 20);
		button4.setBounds(100, 270, 100, 20);
		button5.setBounds(100, 310, 100, 20);
		add(label1);
		add(label2);
		add(label3);
		add(label4);
		add(field1);
		add(field2);
		add(field3);
		add(button1);
		add(button2);
		add(button3);
		add(button4);
		add(button5);
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		button5.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==button1){
			add();
		}
		if(e.getSource()==button2){
			search();
		}
		if(e.getSource()==button3){
			delete();
		}
		if(e.getSource()==button4){
			modify();
		}
		if(e.getSource()==button5){
			ventana.main(null);
		}
	}
	public static void main(String [] args) {	
		ventana ventana=new ventana();
		WriteXMLFile window =new WriteXMLFile();
		window.setBounds(10, 10, 300, 400);
		window.GUI();
		window.position();
		window.setVisible(true);
		window.setResizable(false);
	}
}